<?php

namespace app\api\service;


use app\common\model\UserDressupPackaged;
use think\Log;

class UserDressupPackagedService extends BaseService
{

    /**
     * 回调增加我的套餐装扮数据
     * @param $userId
     * @param $packagedId
     * @param array $dressupIds
     * @return bool
     */
    public function callBack($userId, $packagedId, array $dressupIds)
    {
        $myDressupIds = UserDressupPackaged::where(['user_id' => $userId, 'packaged_id' => $packagedId])->column('dressup_id');

        foreach ($dressupIds as $k => $dressupId) {
            if (in_array($dressupId, $myDressupIds)) {
                unset($dressupIds[$k]);
            }
        }
        if (empty($dressupIds)) {
            return true;
        }

        $dressupPackaged = new UserDressupPackaged();
        $data = [];
        foreach ($dressupIds as $dressupId) {

            $data[] = [
                'user_id' => $userId,
                'packaged_id' => $packagedId,
                'dressup_id' => $dressupId,
                'createtime' => time(),
                'updatetime' => time()
            ];
        }
        $res = $dressupPackaged->insertAll($data);
        if (empty($res)) {
            $this->setMsg('写入用户装扮数据失败！');
            Log::write('写入用户装扮数据失败！' . $res, 'error');
            return false;
        }
        return true;
    }
}
